//
//  SingerShowViewController.m
//  LastTemp
//
//  Created by dllo on 15/4/24.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SingerShowViewController.h"
#import "singerShowCell.h"
#import "singerShowModel.h"
#import "NetWorkingTool.h"
#import "UIImageView+WebCache.h"
#import "DetailSingerViewController.h"
@interface SingerShowViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *singerShowTableView;
@property(nonatomic, retain)NSMutableArray *singerShowArr;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@end

@implementation SingerShowViewController
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.singerShowArr = [NSMutableArray array];
    }
    return self;
}
- (void)dealloc
{
    [_singerShowArr release];
    [_singerShowTableView release];
    [_activity release];
    [super dealloc];
}
// 解析数据
- (void)showData{
    NSString *str = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=%@&size=1000&page=1&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone5C&f=f320&s=s310&imsi=&hid=&splus=8.0.2&active=1&net=2&openudid=50a40f05044a0e2462e11cbf7d6612844b6c0008&idfa=058C286B-AB2C-41E2-A7FE-B67B3496FBBC&utdid=VQKI/UR603ADANyHaLs5uRrS&alf=201200", self.passId];
    [NetWorkingTool netWorkingToolStr:str block:^(id result) {
        NSMutableDictionary *dic = result;
        NSMutableArray *arr = [NSMutableArray array];
        arr = dic[@"data"];
        for (NSInteger i = 0; i < arr.count; i++) {
            singerShowModel *singerShow = [[singerShowModel alloc] init];
            [singerShow setValuesForKeysWithDictionary:arr[i]];
            [self.singerShowArr addObject:singerShow];
            [singerShow release];
        }
        [self.singerShowTableView reloadData];
        [self.activity stopAnimating];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showData];
    self.title = self.passTitle;
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"39"]];
    self.singerShowTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 24, self.view.frame.size.width, self.view.frame.size.height - 44) style:UITableViewStylePlain];
    [self.view addSubview:self.singerShowTableView];
    self.singerShowTableView.backgroundColor = [UIColor clearColor];
    [_singerShowTableView release];
    
    self.singerShowTableView.delegate = self;
    self.singerShowTableView.dataSource = self;
    
    // 进度轮
    self.activity = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    [self.activity setCenter:CGPointMake(self.view.frame.size.width / 2, (self.view.frame.size.height - 113) / 2)]; // 指定进度轮中心点
    [self.activity setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    [_activity release];
}
#pragma mark 协议必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.singerShowArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    singerShowCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (cell == nil) {
        cell = [[[singerShowCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    singerShowModel *singerShow = [[singerShowModel alloc] init];
    singerShow = self.singerShowArr[indexPath.row];
    cell.singerNameLabel.text = singerShow.singer_name;
    NSURL *url = [NSURL URLWithString:singerShow.pic_url];
    [cell.showImageView sd_setImageWithURL:url];
    cell.smallShowImageView.image = [UIImage imageNamed:@"08"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120;
}
// 跳转页面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailSingerViewController *detailSingerVC = [[DetailSingerViewController alloc] init];
    singerShowModel *singerShow = [[singerShowModel alloc] init];
    singerShow = self.singerShowArr[indexPath.row];
    detailSingerVC.passSingerID = singerShow.singer_id;
    detailSingerVC.passSinger_name = singerShow.singer_name;
    detailSingerVC.passPic_url = singerShow.pic_url;
    [self.navigationController pushViewController:detailSingerVC animated:YES];
    [detailSingerVC release];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
